package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.db.DB;

import com.vo.Users;

public class UserDaoImp implements IUserDao {

	Connection con;
	DB db = new DB();
	public boolean login(Users users) throws Exception {
		//得到数据库连接
		
		//连接对象的地址放在了con
		 con = db.getConn();
		//得到一个操作数据库的类
		String sql ="select eu_user_id,eu_password,eu_user_name from easybuy_user where eu_user_name=? and eu_password=?";
		PreparedStatement ps = con.prepareStatement(sql);
		//给?注入值
		ps.setString(1, users.getEu_user_name());
		ps.setString(2, users.getEu_password());
		//操作数据库   
		//查询方法executeQuery()
		ResultSet rs = ps.executeQuery();
		boolean flag = rs.next();
		ps.close();
		DB dbs = new DB();
		dbs.closeConn();
		
		return flag;
	}
	//测试数据库是否能查询数据
	public static void main(String[] args){
		IUserDao userdao = new UserDaoImp();
		List<Users> list = new ArrayList<Users>();
		Users users = new Users();
		try {
			list = userdao.getAllData();
			if(list!=null){
				for(int a = 0;a <list.size();a++){
					users = list.get(a);
					System.out.println(users.getEu_user_id()+"<br>"+users.getEu_user_name()+"<br>"+users.getEu_sex()+"<br>"+users.getEu_email()+"<br>"+users.getEu_mobile());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
//		users.setEu_user_name("name");
//		users.setEu_password("15666");
//		
//		try {
//			userdao.login(users);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		
	}
	public List<Users> getAllData() throws Exception {
		// TODO Auto-generated method stub
		con = db.getConn();
		String sql = "select * from easybuy_user";
		PreparedStatement ps = con.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		List<Users> list = new ArrayList();
		while(rs.next()){
			Users u = new Users();
			u.setEu_user_id(rs.getInt(1));
			u.setEu_user_name(rs.getString(2));
			u.setEu_password(rs.getString(3));
			u.setEu_sex(rs.getString(4));
			u.setEu_email(rs.getString(5));
			u.setEu_mobile(rs.getString(6));
			list.add(u);
			
		}
		rs.close();
		ps.close();
		db.closeConn();
		return list;
	}

}
